# The American Viewer: Political Consequences of Entertainment Media
# Eunji Kim and Shawn Patterson Jr 
# American Political Science Review 
# Replication File [Figure 1]




library(readr)
library(ggplot2)
library(foreign)



df <- read_csv("figure1.csv")
colnames(df) <- c("tvseason", "type", "viewership")


t <- ggplot(df, aes(x=tvseason, y=viewership, fill=type)) +
  geom_bar(stat="identity", position=position_dodge2(width =1, preserve = "single")) + 
  scale_fill_manual(breaks= c("NBC Evening News", "The Apprentice"), values=c('grey75','#8a0002', '#8a0002')) +
  theme_minimal () + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                           panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  theme(axis.text.x=element_text(colour="black", size=12), axis.text.y=element_text(colour="black", size=12))  +
  theme(legend.title = element_blank(), legend.text=element_text(color="black", size=12), legend.position = c(0.9, 0.9), legend.box = "horizontal") +
  ylab("Viewership (millions)") + xlab("TV Season (2003-2015)") + theme(axis.title=element_text(size=14,face="bold")) + 
facet_grid(. ~ tvseason, scales = "free_x", space = "free") +  theme(strip.text.x = element_blank()) + 
  scale_y_continuous(breaks = seq(0, 22, by = 4)) 



colour_mapping <- c("NBC Evening News" = 'grey75', 
                    "The Apprentice" = '#8a0002', 
                    "The Apprentice 2" = '#8a0002')

# A vector of legend labels
labels <- c("NBC Evening News", "The Apprentice")

ggplot(df, aes(x=tvseason, y=viewership, fill=type)) +
  geom_bar(stat="identity", position=position_dodge2(width =1, preserve = "single")) + 
  scale_fill_manual(values = colour_mapping, 
                    breaks = labels,
                    guide = guide_legend(override.aes = list(fill = colour_mapping[labels]))) +
  theme_minimal () + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                           panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  theme(axis.text.x=element_text(colour="black", size=12), axis.text.y=element_text(colour="black", size=12))  +
  theme(legend.title = element_blank(), legend.text=element_text(color="black", size=12), 
        legend.position = c(0.9, 0.9), legend.box = "horizontal") +
  ylab("Viewership (millions)") + xlab("TV Season (2003-2015)") + 
  theme(axis.title=element_text(size=14,face="bold")) + 
  facet_grid(. ~ tvseason, scales = "free_x", space = "free") +  
  theme(strip.text.x = element_blank()) + 
  scale_y_continuous(breaks = seq(0, 22, by = 4))



# exporting the figure 

jpeg("figure1.jpg", units="in", width=11, height=5, res=300)

ggplot(df, aes(x=tvseason, y=viewership, fill=type)) +
  geom_bar(stat="identity", position=position_dodge2(width =1, preserve = "single")) + 
  scale_fill_manual(values = colour_mapping, 
                    breaks = labels,
                    guide = guide_legend(override.aes = list(fill = colour_mapping[labels]))) +
  theme_minimal () + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                           panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  theme(axis.text.x=element_text(colour="black", size=12), axis.text.y=element_text(colour="black", size=12))  +
  theme(legend.title = element_blank(), legend.text=element_text(color="black", size=12), 
        legend.position = c(0.9, 0.9), legend.box = "horizontal") +
  ylab("Viewership (millions)") + xlab("TV Season (2003-2015)") + 
  theme(axis.title=element_text(size=14,face="bold")) + 
  facet_grid(. ~ tvseason, scales = "free_x", space = "free") +  
  theme(strip.text.x = element_blank()) + 
  scale_y_continuous(breaks = seq(0, 22, by = 4))


dev.off() 









########## previous graph #########


p <- ggplot(data=viewership, aes(x=Season, y=Viewership, fill=Final)) +
  geom_bar(stat="identity", position=position_dodge())+
  geom_text(aes(label=Viewership), vjust=-0.5, color="black",
            position = position_dodge(1), size=4)+
  scale_fill_manual(values=c('red4','red1'))+ scale_x_continuous(breaks=seq(1, 14, 1)) +
  theme_minimal () + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                    panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  theme(axis.text.x=element_text(colour="black", size=12), axis.text.y=element_text(colour="black", size=12))  +
  theme(legend.title = element_blank(), legend.text=element_text(color="black", size=12), legend.position = c(0.9, 0.9), legend.box = "horizontal") +
  ylab("Viewership (millions)") + xlab("Season") + theme(axis.title=element_text(size=14,face="bold"))
  
ggsave(filename="viewership.pdf", plot=p)




theme(axis.text=element_text(size=12),
      axis.title=element_text(size=14,face="bold"))

heme(legend.text=element_text(color=fill,size=12))
 
heme(axis. text. x = element_text(angle = 90))